#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time          : 2025/3/20 16:42
# File          : post_get_request
# @Author       : MingTai
# @Department   : csc_qa


import requests
import json


#  请求接口打印结果
def send_request_core_query():
    # 请求的 URL
    url = "https://www.baidu.com/query"
    context_str_uid = 111
    # 请求参数,示例，每个参数都是不同的
    payload = {
        "sessionId": "字符串",
        "query_text": 111,
        "clickInfo": None,
        "queryType": "TEXT",
        "context": context_str_uid,
        "debug": True,
        "channel": "",
        "sources": [{'qq': '你好'}, {'aa': 123}],
        "webhookMock": "{\"action\":false,\"webhook\":[]}",
        "botId": "bot_id"
    }

    # 请求头,使用示例
    headers = {
        "Content-Type": "application/json",
        "agentid": "4f831a3d2e50",
        'cookie':'抓包获取对应的cookie',
        "referer": "请求headers的其中一个参数",
        # 添加其他扩展字段
        "User-Agent": "Your User Agent Here",
        "Accept": "application/json",
        # 可以根据需要添加其他 headers
    }

    # 发送 POST 请求，设置 timeout 为 10 秒
    try:
        response = requests.post(url, headers=headers, data=json.dumps(payload), timeout=10)
        result_data = response.json()

        # 输出响应
        print("Status Code:", response.status_code)
        print(json.dumps(response.json(), ensure_ascii=False))

        # 取响应值，子层级的值
        print(f'sessionId = {result_data.get("data").get("sessionId")}')
        print(f'answerText = {result_data.get("data").get("answer").get("answerText")}')


    except requests.exceptions.Timeout:
        print("请求超时，请稍后重试。")
    except requests.exceptions.RequestException as e:
        print("发生错误:", e)


if __name__ == '__main__':
    send_request_core_query()
    context = {
        'usserId': 111,
        'isOnline': 'true',
    }
    print(json.loads(json.dumps(context)))
    print(str(json.dumps(context)))